package com.csc.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csc.bean.RequestResetPassBean;
import com.csc.bean.SystemAccountBean;
import com.csc.dao.SystemRequestDAO;
import com.csc.domain.SystemAccount;
import com.csc.domain.TransactionHistory;

import constants.Constant;

@Service
public class SystemRequestServiceImpl implements SystemRequestService {
	@Autowired
	SystemRequestDAO requestDAO;

	static Logger logger = Logger.getLogger(SystemAccountService.class);

	// Truong Le Bao
	@Override
	public boolean requestResetPassword(String username, String reason) {
		// TODO Auto-generated method stub

		boolean result = requestDAO.requestResetPassword(username, reason);

		return result;
	}

	// Truong Le Bao
	@Override
	public boolean processRequestResetPassword(String requestID,
			String userImpact, String userAffect, int status, String path) {
		// TODO Auto-generated method stub

		try {
			// Add Current Date
			Date currentDate = new Date();
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					Constant.SIMPLE_DATE_FORMAT);
			String current = dateFormat.format(currentDate);
			// End - Add Current Date

			String encode = "", newPassword = "";
			
			if (status == 5) {
				newPassword = PasswordController.generateRandomString(8);
				encode = PasswordController.encodePassword(newPassword);
				String content = "";
				content += "Username: " + userAffect + "\n";
				content += "Date of Request: " + currentDate + "\n";
				content += "New Password: " + newPassword + "\n";
				content += "Encrypted Password: " + encode + "\n";
				content += "**************************************\n\n\n";
				// Export Password File
				String fileName = userAffect + ".txt";

				File destination = new File("C:/Users/cscadmin/Desktop/requests/" + fileName);
				PasswordController.exportPasswordFile(content, destination);
				// End - Export Password File
			}

			return requestDAO.processRequestResetPassword(requestID, encode,
					current, userImpact, userAffect, status);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
	}

	// Truong Le Bao
	@Override
	public boolean processRequestUpdateProfile(SystemAccountBean acc,
			String userImpact) {
		// TODO Auto-generated method stub
		return requestDAO.processRequestUpdateProfile(acc, userImpact);
	}

	// Truong Le Bao
	@Override
	public List<SystemAccountBean> parseToUserProfiles(
			List<SystemAccount> listAll) {
		List<SystemAccountBean> listUserProfile = new ArrayList<SystemAccountBean>();

		String userName = "";
		String role = "";
		String firstName = "";
		String midName = "";
		String lastName = "";
		String email = "";
		String status = "";

		for (SystemAccount sysAcc : listAll) {
			userName = sysAcc.getAccountLoginId();
			if (userName.equals("admin")) {
				continue;
			}
			firstName = sysAcc.getAccountFirstName();
			midName = sysAcc.getAccountMidName();
			lastName = sysAcc.getAccountLastName();
			role = sysAcc.getSystemAccountType().getTypeName();
			email = sysAcc.getAccountEmail1();
			status = sysAcc.getSystemStatus().getStatusName();
			listUserProfile.add(new SystemAccountBean(role, userName,
					firstName, lastName, midName, email, status));
		}
		return listUserProfile;
	}

	// Truong Le Bao
	@Override
	public List<RequestResetPassBean> parseToRequestDetail(
			List<TransactionHistory> listTransactionReset) {
		// TODO Auto-generated method stub

		int transansactionID = 0;
		String username, role, firstName, midName, lastName, date = "";

		List<RequestResetPassBean> listRequestJson = new ArrayList<RequestResetPassBean>();

		for (TransactionHistory transactionHistory : listTransactionReset) {
			transansactionID = transactionHistory.getTransactionId();
			username = transactionHistory.getAccImpact().getAccountLoginId();
			role = transactionHistory.getAccImpact().getSystemAccountType()
					.getTypeName();
			firstName = transactionHistory.getAccImpact().getAccountFirstName();
			midName = transactionHistory.getAccImpact().getAccountMidName();
			lastName = transactionHistory.getAccImpact().getAccountLastName();
			date = transactionHistory.getDateTransaction();

			listRequestJson.add(new RequestResetPassBean(transansactionID,
					username, role, firstName, midName, lastName, date));
		}

		return listRequestJson;
	}

	// Truong Le Bao
	@Override
	public TransactionHistory getTransactionByID(int ID) {
		// TODO Auto-generated method stub
		return requestDAO.getTransactionByID(ID);
	}

	// Truong Le Bao
	@Override
	public List<TransactionHistory> searchAdvanceNewRequests(String fullname,
			String username, String role, String from, String to) {
		// TODO Auto-generated method stub

		return requestDAO.searchAdvanceNewRequests(fullname, username, role,
				from, to);
	}

	// Truong Le Bao
	@Override
	public List<RequestResetPassBean> getNewRequests() {
		// TODO Auto-generated method stub
		List<TransactionHistory> listTransaction = requestDAO
				.getListNewTransactionReset();

		return parseToRequestDetail(listTransaction);
	}

	@Override
	public boolean isAccountStatusRequestAvailable(String username) {
		// TODO Auto-generated method stub
		return requestDAO.isAccountStatusRequestAvailable(username);
	}
}
